package com.flyduck.mall.pms.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.pms.admin.vo.*;
import com.flyduck.mall.pms.domain.PmsSpecification;
import com.flyduck.mall.pms.mapper.PmsSpecificationMapper;
import com.flyduck.ss.common.core.utils.StringUtils;
import com.flyduck.mall.common.mybatis.utils.PageUtils;
import com.flyduck.mall.pms.admin.service.AdminSpecificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * AdminSpecificationServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-06-05
 */
@Service
public class AdminSpecificationServiceImpl implements AdminSpecificationService {

    @Resource
    private PmsSpecificationMapper pmsSpecificationMapper;

    @Override
    public Page<AdminSpecificationRespVO> getSpecificationPage(Page page, AdminSpecificationQueryReqVO adminSpecificationQueryReqVO) {
        Page<PmsSpecification> appSpecificationPage = pmsSpecificationMapper.selectPage(page,
                new LambdaQueryWrapper<PmsSpecification>()
                        .like(StringUtils.isNotBlank(adminSpecificationQueryReqVO.getName()), PmsSpecification::getName, adminSpecificationQueryReqVO.getName())
                        .orderByDesc(PmsSpecification::getSortOrder, PmsSpecification::getCreateTime)
        );

        return PageUtils.convert(appSpecificationPage, AdminSpecificationRespVO.class);
    }

    @Override
    public AdminSpecificationDetailsRespVO getSpecificationDetailsById(Long id) {
        PmsSpecification pmsSpecification = pmsSpecificationMapper.selectById(id);
        AdminSpecificationDetailsRespVO adminSpecificationDetailsRespVO = BeanUtils.toBean(pmsSpecification, AdminSpecificationDetailsRespVO.class);
        if(StringUtils.isBlank(adminSpecificationDetailsRespVO.getOptions())){
            adminSpecificationDetailsRespVO.setOptions("");
        }
        return adminSpecificationDetailsRespVO;
    }

    @Override
    public void saveSpecification(AdminSpecificationSaveReqVO adminSpecificationSaveReqVO) {
        PmsSpecification pmsSpecification = BeanUtils.toBean(adminSpecificationSaveReqVO, PmsSpecification.class);
        pmsSpecification.setCreateTime(new Date());
        pmsSpecificationMapper.insert(pmsSpecification);
    }

    @Override
    public void updateSpecification(AdminSpecificationUpdateReqVO adminSpecificationUpdateReqVO) {
        PmsSpecification pmsSpecification = BeanUtils.toBean(adminSpecificationUpdateReqVO, PmsSpecification.class);
        pmsSpecification.setUpdateTime(new Date());
        pmsSpecificationMapper.updateById(pmsSpecification);
    }

    @Override
    public void batchDeleteByIds(List<Long> ids) {
        pmsSpecificationMapper.deleteBatchIds(ids);
    }
}
